<template>
    <div class="register-container">
      <div class="register-box">
        <div class="register-header">
          <img src="../assets/OIP-C.jpg" alt="玲珑AI" class="logo">
          <h1>注册玲珑AI账号</h1>
        </div>
  
        <el-form 
          :model="registerForm" 
          :rules="rules" 
          ref="registerFormRef"
          label-position="top"
        >
          <el-form-item label="用户名" prop="username">
            <el-input 
              v-model="registerForm.username"
              :prefix-icon="User"
              placeholder="请设置用户名"
            />
          </el-form-item>
  
          <!-- <el-form-item label="邮箱" prop="email">
            <el-input 
              v-model="registerForm.email"
              :prefix-icon="Message"
              placeholder="请输入邮箱"
            />
          </el-form-item> -->
  
          <el-form-item label="密码" prop="password">
            <el-input 
              v-model="registerForm.password"
              :prefix-icon="Lock"
              type="password"
              placeholder="请设置密码"
              show-password
            />
          </el-form-item>
  
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input 
              v-model="registerForm.confirmPassword"
              :prefix-icon="Lock"
              type="password"
              placeholder="请再次输入密码"
              show-password
            />
          </el-form-item>
  
          <!-- <el-form-item prop="agreement">
            <el-checkbox v-model="registerForm.agreement">
              我已阅读并同意
              <el-link type="primary" :underline="false">服务条款</el-link>
              和
              <el-link type="primary" :underline="false">隐私政策</el-link>
            </el-checkbox>
          </el-form-item> -->
  
          <el-button 
            type="primary" 
            class="register-button" 
            @click="handleRegister" 
            :loading="loading"
          >
            立即注册
          </el-button>
  
          <div class="login-link">
            已有账号？ <el-link type="primary" :underline="false" @click="goToLogin">返回登录</el-link>
          </div>
        </el-form>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import { User, Lock, Message } from '@element-plus/icons-vue'
  import { useUserStore } from '@/store/modules/user'

  const userStore = useUserStore()  
  const router = useRouter()
  const loading = ref(false)
  const registerFormRef = ref()
  
  const registerForm = reactive({
    username: 'newuser222',
    // email: '',
    password: 'secret123',
    confirmPassword: 'secret123',
    // agreement: false
  })
  
  const validatePass = (rule, value, callback) => {
    if (value === '') {
      callback(new Error('请输入密码'))
    } else {
      if (registerForm.confirmPassword !== '') {
        registerFormRef.value.validateField('confirmPassword')
      }
      callback()
    }
  }
  
  const validatePass2 = (rule, value, callback) => {
    if (value === '') {
      callback(new Error('请再次输入密码'))
    } else if (value !== registerForm.password) {
      callback(new Error('两次输入密码不一致'))
    } else {
      callback()
    }
  }
  
  const rules = {
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
    ],
    email: [
      { required: true, message: '请输入邮箱地址', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
    ],
    password: [
      { required: true, validator: validatePass, trigger: 'blur' },
      { min: 6, message: '密码长度至少为6个字符', trigger: 'blur' }
    ],
    confirmPassword: [
      { required: true, validator: validatePass2, trigger: 'blur' }
    ],
    // agreement: [
    //   { 
    //     validator: (rule, value, callback) => {
    //       if (!value) {
    //         callback(new Error('请阅读并同意服务条款和隐私政策'))
    //       } else {
    //         callback()
    //       }
    //     }, 
    //     trigger: 'change'
    //   }
    // ]
  }
  
  const handleRegister = async () => {
    if (!registerFormRef.value) return
    
    await registerFormRef.value.validate(async (valid) => {
      if (valid) {
        loading.value = true
        try {
          // 这里添加注册API调用
          await userStore.register(registerForm)
          ElMessage.success('注册成功！')
          router.push('/login')
        } catch (error) {
          ElMessage.error('注册失败，请稍后重试')
        } finally {
          loading.value = false
        }
      }
    })
  }
  
  const goToLogin = () => {
    router.push('/login')
  }
  </script>
  
  <style scoped>
  .register-container {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  }
  
  .register-box {
    width: 460px;
    padding: 40px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  }
  
  .register-header {
    text-align: center;
    margin-bottom: 40px;
  }
  
  .logo {
    width: 64px;
    height: 64px;
    margin-bottom: 16px;
  }
  
  .register-header h1 {
    font-size: 24px;
    color: #333;
    margin: 0;
  }
  
  .register-button {
    width: 100%;
    padding: 12px;
    margin: 20px 0;
  }
  
  .login-link {
    text-align: center;
    margin: 16px 0;
  }
  
  :deep(.el-form-item__label) {
    padding-bottom: 4px;
  }
  
  :deep(.el-input__wrapper) {
    padding: 12px;
  }
  
  :deep(.el-form-item) {
    margin-bottom: 20px;
  }
  </style>
  